import 'dart:typed_data';

import 'package:aichat_flutter_wallet/module/module_news/widget/new_commemt_widget.dart';
import 'package:aichat_flutter_wallet/module_common/provider/nav_key.dart';
import 'package:flutter/material.dart';
import 'package:lib_language/lib_language.dart';
import 'package:video_thumbnail/video_thumbnail.dart';
import 'package:path/path.dart' as path;

const monthList = [
  '',
  'Jan',
  'Feb',
  'Mar',
  'Apr',
  'May',
  'Jun',
  'Jul',
  'Aug',
  'Sept',
  'Oct',
  'Nov',
  'Dec'
];

class NewsUtil {
  static String transDate(String date) {
    int timestamp = int.parse(date);
    if (date.length != 13) {
      timestamp = timestamp * 1000;
    }
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
    // print('month==${dateTime.month}');
    String formattedDate = DateFormat('d, yyyy HH:mm').format(dateTime);
    String value = monthList[dateTime.month] + " " + formattedDate;
    return value;
  }

  static String transDayDate(String date) {
    int timestamp = int.parse(date);
    if (date.length != 13) {
      timestamp = timestamp * 1000;
    }
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
    String formattedDate = DateFormat('yyyy-HH-mm').format(dateTime);
    return formattedDate;
  }

  // 格式化为240320格式
  static String transCurDate() {
    DateTime now = DateTime.now();
    // 格式化为240320格式
    String formattedDate =
        "${now.year.toString().substring(2)}${now.month.toString().padLeft(2, '0')}${now.day.toString().padLeft(2, '0')}";
    return formattedDate;
  }

  // 判断文件类型
  static int getFileType(String filePath) {
    String extension = path.extension(filePath);
    // 判断文件类型
    if (extension == '.mp4' ||
        extension == '.avi' ||
        extension == '.mov' ||
        extension == '.wmv') {
      return 2;
    } else if (extension == '.jpg' ||
        extension == '.jpeg' ||
        extension == '.png' ||
        extension == '.gif') {
      return 1;
    } else {
      return 0;
    }
  }

// 去除多余的参数
  static String removeQueryParameters(String url) {
    int questionMarkIndex = url.indexOf('?');
    return (questionMarkIndex != -1)
        ? url.substring(0, questionMarkIndex)
        : url;
  }

  // 名字过长截取
  static String getName(String name, {int maxLength = 15}) {
    if (name.length > maxLength) {
      return name.substring(0, 10)+'...';
    } else {
      return name;
    }
  }

  static Future<Uint8List> getVideoThumb(String video) async {
    final uint8list = await VideoThumbnail.thumbnailData(
      video: video,
      imageFormat: ImageFormat.JPEG,
      maxWidth: 500, // 可选参数，限制宽度以保持纵横比
      quality: 100, // 可选参数，用于设置 JPEG 压缩质量（0-100）
    );

    return uint8list!;
  }

  static Future<String> getVideoPickerThumb(String path) async {
    final thumbnailPath = await VideoThumbnail.thumbnailFile(
      video: path,
      imageFormat: ImageFormat.JPEG,
      maxWidth: 500, // 可选参数，限制宽度以保持纵横比
      quality: 100, // 可选参数，用于设置 JPEG 压缩质量（0-100）
    );

    return thumbnailPath!;
  }

  // 评论弹窗
  static showCommentDialog({
    required BuildContext ctx,
    Function? success,
    Function? fail,
    String toName ='',
    VoidCallback? closeCallback = null,
  }) {
    return showModalBottomSheet(
        context: ctx,
        backgroundColor: Colors.transparent,
        isScrollControlled: true,
        enableDrag: false,
        useSafeArea: false,
        builder: (_) {
          return NewCommemtWidget(
            toName: toName,
            closeCallback: closeCallback,
          );
        }).then((value) {
      if (value != null) {
        if (success != null) success(value);
      } else {
        if (fail != null) fail();
      }
    });
  }
}
